package memconf

import (
	"errors"
	"sync"

	"gitee.com/haodreams/libs/config"
)

// Conf 读取配置文件
type Conf struct {
	Lock *sync.RWMutex
	conf *config.Map
}

func NewConf() *Conf {
	c := new(Conf)
	c.Lock = new(sync.RWMutex)
	c.conf = config.NewMap()
	return c
}

func NewFromMap(m map[string]string) *Conf {
	c := NewConf()
	c.conf.SetMap(m)
	return c
}

// Set 重设指定的Key Value
func (conf *Conf) Set(key, val string) (err error) { //support section::key type in given key when using ini type.
	if len(key) == 0 {
		return errors.New("key is empty")
	}
	conf.Lock.Lock()
	defer conf.Lock.Unlock()

	return conf.conf.Set(key, val)
}

// String Get string value
func (conf *Conf) String(key string) (s string) { //support section::key type in key string when using ini and json type; Int,Int64,Bool,Float,DIY are same.
	conf.Lock.RLock()
	defer conf.Lock.RUnlock()
	return conf.conf.String(key)
}

// Strings 没实现
func (conf *Conf) Strings(key string) (ss []string) { //get string slice
	return
}

// Int Get int value by key
func (conf *Conf) Int(key string) (int, error) {
	conf.Lock.RLock()
	defer conf.Lock.RUnlock()
	return conf.conf.Int(key)
}

// Int64 Get int64 value by key
func (conf *Conf) Int64(key string) (int64, error) {
	conf.Lock.RLock()
	defer conf.Lock.RUnlock()
	return conf.conf.Int64(key)
}

// Bool Get bool value by key
func (conf *Conf) Bool(key string) (bool, error) {
	conf.Lock.RLock()
	defer conf.Lock.RUnlock()
	return conf.conf.Bool(key)
}

// Float Get float64 by key
func (conf *Conf) Float(key string) (float64, error) {
	conf.Lock.RLock()
	defer conf.Lock.RUnlock()
	return conf.conf.Float(key)
}

// DefaultString Get value by key, if value is not exist, return defaultValue
func (conf *Conf) DefaultString(key string, defaultVal string) string { // support section::key type in key string when using ini and json type; Int,Int64,Bool,Float,DIY are same.
	conf.Lock.RLock()
	defer conf.Lock.RUnlock()

	return conf.conf.DefaultString(key, defaultVal)
}

// DefaultStrings Not implementation
func (conf *Conf) DefaultStrings(key string, defaultVal []string) []string {
	return nil
}

// DefaultInt Get int value by key, if value is not exist, return defaultValue
func (conf *Conf) DefaultInt(key string, defaultVal int) int {
	conf.Lock.RLock()
	defer conf.Lock.RUnlock()
	return conf.conf.DefaultInt(key, defaultVal)
}

// DefaultInt64 int64 value by key, if value is not exist, return defaultValue
func (conf *Conf) DefaultInt64(key string, defaultVal int64) int64 {
	conf.Lock.RLock()
	defer conf.Lock.RUnlock()
	return conf.conf.DefaultInt64(key, defaultVal)
}

// DefaultBool int64 value by key, if value is not exist, return defaultValue
func (conf *Conf) DefaultBool(key string, defaultVal bool) bool {
	conf.Lock.RLock()
	defer conf.Lock.RUnlock()
	return conf.conf.DefaultBool(key, defaultVal)
}

// DefaultFloat float64 value by key, if value is not exist, return defaultValue
func (conf *Conf) DefaultFloat(key string, defaultVal float64) float64 {
	conf.Lock.RLock()
	defer conf.Lock.RUnlock()
	return conf.conf.DefaultFloat(key, defaultVal)
}

// DIY Not implementation
// TODO 暂没实现
func (conf *Conf) DIY(key string) (interface{}, error) {
	return nil, nil
}

// GetSection Not implementation
func (conf *Conf) GetSection(section string) (map[string]string, error) {
	return nil, nil
}

// SaveConfigFile Save config
func (conf *Conf) SaveConfigFile(filename string) error {
	return nil
}

//func NewConfig(reserve, key string) (c config.Configer, err error) {
//	conf := new(Conf)
//	return conf.Parse(key)
//}

// Parse 从关系库表中读取配置
func (conf *Conf) Parse(key string) (c config.Configer, err error) {
	return
}

// ParseData Not implementation
func (conf *Conf) ParseData(data []byte) (config.Configer, error) {
	return conf, errors.New("not support")
}
